package cn.usian.service.Impl;

import cn.usian.common.R;
import cn.usian.dao.EmpDao;
import cn.usian.entity.Employee;
import cn.usian.service.EmpService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;



import javax.annotation.Resource;

@Service
@Transactional//声明式事物
public class EmpServiceImpl implements EmpService {
    @Resource
    private EmpDao empDao;

    @Override
    public R<Employee> login(Employee employee) {
        //1.检查用户输入的用户名是否存在==》根据用户名来查询用户的信息
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee selectOne = empDao.selectOne(queryWrapper);
        //执行查询
      if (selectOne==null){
          return  R.error("用户不存在");
      }
        String hex = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        if (hex.equals(selectOne.getPassword())){
            return  R.error("密码不正确");
        }
        if (selectOne.getStatus()==0){
            return  R.error("账号已被禁用");
        }
        return R.success(employee);
    }



    @Override
    public R<Page> page(int page, int pageSize, String name) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,Employee::getName,name);
        Page<Employee> page1 = new Page<>(page,3);
        Page<Employee> employeePage = empDao.selectPage(page1, queryWrapper);
        return R.success(employeePage);
    }
}
